const db = require("../config/mysql_util");
class CartModel {
  constructor() {}
  findByUserId(userid) {
    return db.query(`select c.*, g.goods_price, g.goods_name, g.goods_small_logo,
            (g.goods_price *c.goods_num) total from cart c inner join goods g on g.goods_id = c.goods_id where c.user_id = ?`, [userid]);
  }
  select(user_id, goods_id){
    return db.query("select * from cart where user_id = ? and goods_id = ?", [user_id, goods_id]);
  }
  insert(data) {
    return db.query("insert into cart set ?", [data]);
  }
  update(data, user_id, cart_id) {
    return db.query("update cart set ? where user_id = ? and cart_id = ?", [data, user_id, cart_id]);
  }
  delById(user_id, goods_id){
    return db.query(`delete from cart where user_id = ? and goods_id in (${goods_id})`, [user_id]);
  }
}

module.exports = new CartModel();